import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginAPI } from '@/api/user'
import { useCartStore } from '@/stores/cartStore'
import{mergeCartApi}from '@/api/cart'


export const useUserStore = defineStore(
  'user',
  () => {
    const cartStore = useCartStore()
    const userInfo = ref({})
    const getUserInfo = async (i) => {
      const res = await loginAPI(i)
      userInfo.value = res.result
     await mergeCartApi(cartStore.cartList.map(item=>{
        return{
          skuId:item.skuId,
          count:item.count,
          selected:item.selected
        }
      }))
      cartStore.updateCartList()
    }
    const clearUserInfo = () => {
      userInfo.value = {}
      cartStore.clearCart()
    }
    return {
      userInfo,
      getUserInfo,
      clearUserInfo
    }
  },
  {
    persist: true
  }
)
